package com.jshan.blog.adapter;

import com.jshan.blog.utils.FileUploadUtils;
import com.jshan.blog.utils.MinioUtil;
import io.minio.ObjectWriteResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
public class MinioStorageAdapter implements StorageAdapter {

    @Resource
    private MinioUtil minioUtil;

    @Override
    public String uploadFile(MultipartFile file, String type, String storageType) {
        String fileName = type + "/" + FileUploadUtils.generateFileName("", file.getOriginalFilename(), storageType);
        // 上传
        ObjectWriteResponse objectWriteResponse = minioUtil.uploadFile(file, fileName, file.getContentType());
        // 获取上传 url
        String filUrl = minioUtil.getPublicObjectUrl(objectWriteResponse.object());
        return filUrl;
    }
}
